<template>
  <div class="mod-config crmtagLead">
    <a-breadcrumb class="location" style="margin-bottom: 13px;">
      <a-breadcrumb-item>System</a-breadcrumb-item>
      <a-breadcrumb-item>tags</a-breadcrumb-item>
    </a-breadcrumb>

    <el-row :gutter="10" style="margin-bottom: 20px;">
      <el-col :span="2" style="width: 120px; padding:5px;">
        <el-button style="float:left; width: 100px; height: 36px; background:rgba(24,144,255,1); border-radius:2px;border: 0;" v-if="isAuth('crm:crmtags4leads:save')" type="primary" @click="addOrUpdateHandle()">create</el-button>
        </el-col>
        <el-col :span="2" style="width: 160px; padding:5px 0;" >
          <el-select v-model="fType" @change="getDataList">
            <el-option value="all"> all </el-option>
            <el-option v-for="(item,index) in leadType" :key="index" :value="item"> {{ item }} </el-option>
          </el-select>
        </el-col>
    </el-row>

    <el-table
      :data="dataList"      border      v-loading="dataListLoading"      @selection-change="selectionChangeHandle"      style="width: 100%;">
      <el-table-column        type="selection"        header-align="center"        align="center"        width="50" />
      <el-table-column   width="160"     prop="type"        header-align="center"        align="center"        label="type" />
      <!-- <el-table-column        prop="tagId"        header-align="center"        align="center"        label="" /> -->
      <el-table-column        prop="tag"        header-align="center"        align="left"        label="tags" />
      <el-table-column    width="60"    prop="ordindex"        header-align="center"        align="center"        label="order" />
      <!-- <el-table-column        prop="leadsId"        header-align="center"        align="center"        label="" /> -->
      <el-table-column        prop="remark"        header-align="center"        align="center"        label="remark" />
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="operate">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.tagId)" style="color: #1890FF;">modify</el-button>
          <!-- <el-button type="text" size="small" @click="deleteHandle(scope.row.tagId)">delete</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './crmtags4leads-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        leadType:[],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        fType:'all'
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        // console.log(this.fType)

        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/crm/crmtags4leads/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'fType':this.fType
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.leadType=data.leadType
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.tagId
        })
        // this.$confirm4elmt(`${view_tip_confirm_1}[id=${ids.join(',')}]${view_tip_confirm_2}[${id ? '${view_tip_confirm_3}' : '${view_tip_confirm_4}'}]${view_tip_confirm_5}?`
        //         , '${view_tip_txt}'
        //         , {
        //   confirmButtonText: 'Yes',
        //   cancelButtonText: 'Cancel',
        //   type: 'warning'
        // }).then(() => {
        //   this.$http({
        //     url: this.$http.adornUrl('/crm/crmtags4leads/delete'),
        //     method: 'post',
        //     data: this.$http.adornData(ids, false)
        //   }).then(({data}) => {
        //     if (data && data.code === 0) {
        //       this.$message({
        //         message: 'Success',
        //         type: 'success',
        //         duration: 1500,
        //         onClose: () => {
        //           this.getDataList()
        //         }
        //       })
        //     } else {
        //       this.$message.error(data.msg)
        //     }
        //   })
        // })
        var _this = this
        _this.$confirm({//这个confirm是antUI的
        title: `${view_tip_confirm_1}[id=${ids.join(',')}]${view_tip_confirm_2}[${id ? '${view_tip_confirm_3}' : '${view_tip_confirm_4}'}]${view_tip_confirm_5}?`,
        content: '${view_tip_txt}',
        onOk() {
          console.log('----- 删除 -----');
          _this.$http({
            url: _this.$http.adornUrl('/crm/crmtags4leads/delete'),
            method: 'post',
            data: _this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              _this.$message({
                message: 'Success',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  _this.getDataList()
                }
              })
            } else {
              _this.$message.error(data.msg)
            }
          })
        },
        onCancel() {
          console.log('----- 取消删除 -----');
        }
      })
      }
    }
  }
</script>

<style lang="scss">
.el-select .el-input__inner {
  height: 36px;
  background:rgba(243,246,253,1);
  border-radius:2px;
}
.el-pager li.active {
  color: #1890FF;
}
</style>
